package com.cjw.campus.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.ClassTable;
import com.cjw.campus.system.service.IClassTableService;
import com.cjw.campus.system.vo.ClassTableVO;
import core.result.JsonResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-11-21
 */
@RestController
@RequestMapping("/system/classTable")
@Api(tags = "班级管理", value = "班级管理接口")
public class ClassTableController {

    @Autowired
    private IClassTableService classTableService;

    @PostMapping("/Save")
    @ApiOperation(value = "保存班级信息", notes = "保存班级信息", httpMethod = "POST")
    @ApiParam(name = "classTable", value = "班级实体类", required = true)
    public JsonResult save (ClassTable classTable) {
        // 保存班级信息
        boolean save = classTableService.save(classTable);
        // 判断班级是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id更新班级信息", notes = "根据id更新班级信息", httpMethod = "POST")
    @ApiParam(name = "classTable", value = "班级实体类", required = true)
    public JsonResult updateById (ClassTable classTable) {
        boolean updateById = classTableService.updateById(classTable);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除班级信息", notes = "根据id删除班级信息", httpMethod = "GET")
    @ApiParam(name = "classId", value = "班级ID", required = true)
    public JsonResult removeById (Long classId) {
        boolean removeId = classTableService.removeById(classId);
//         判断用户是否删除成功
        if (removeId) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除班级信息", notes = "根据id批量删除班级信息", httpMethod = "POST")
    @ApiParam(name = "classId", value = "班级ID", required = true)
    public JsonResult removeByIds (Long[] idList) {
        boolean removeSchoolIds = classTableService.removeByIds(Arrays.asList(idList));
        if (removeSchoolIds) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @GetMapping("/listPageByClass")
    @ApiOperation(value = "分页查询班级信息", notes = "分页查询班级信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2")
    })
    public JsonResult listPageByClass (Integer currentPage,Integer pageSize) {
        IPage<ClassTableVO> page = new Page<>(currentPage, pageSize);
        IPage<ClassTableVO> listPageByClass = classTableService.listPageByClass(page);
        return JsonResult.ok(listPageByClass);
    }

    @GetMapping("/listPageByClassCodeAndName")
    @ApiOperation(value = "模糊分页查询班级信息", notes = "模糊分页查询班级信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "classCode", value = "班级编码", dataType = "String"),
            @ApiImplicitParam(name = "className", value = "班级名称", dataType = "String"),
    })
    public JsonResult listPageByClassCodeAndName (Integer currentPage,Integer pageSize,String classCode, String className) {
        IPage<ClassTableVO> page = new Page<>(currentPage, pageSize);
        IPage<ClassTableVO> mapPage = classTableService.listPageByClassCodeAndName(page, classCode, className);
        return JsonResult.ok(mapPage);
    }

    @GetMapping("/listByClassId")
    @ApiOperation(value = "根据id查询班级信息", notes = "根据id查询班级信息", httpMethod = "GET")
    @ApiParam(name = "classId", value = "学校ID", required = true)
    public JsonResult listByClassId (Long classId) {
        ClassTableVO listByClassId = classTableService.listByClassId(classId);
        return JsonResult.ok(listByClassId);
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询所有班级信息", notes = "查询所有班级信息", httpMethod = "GET")
    public JsonResult list () {
        List<ClassTable> list = classTableService.list();
        return JsonResult.ok(list);
    }

}
